﻿using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Frm.NodeTree
{
    public partial class MembershipConfig : BaseForm
    {
        /// <summary>
        /// 传入的页面 0代表逻辑分段 1代表案件要素中心
        /// </summary>
        public int TabName { get; set; }

        /// <summary>
        /// 段落code或者要素xmlPath
        /// </summary>
        public string NodeName { get; set; }

        /// <summary>
        /// 模块编号
        /// </summary>
        public string MudelName { get; set; }

        /// <summary>
        /// 2019-10-25 lingshi
        /// </summary>
        public Tuple<string, string> Tuples { get; set; }

        /// <summary>
        /// 文件名称
        /// </summary>
        public string FileName { get; set; }

        ExtractFeatureBLL efBll = new ExtractFeatureBLL();

        public MembershipConfig()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取选中的表
        /// </summary>
        public DataTable CheckTable { get; set; }
        DataTable dt = new DataTable();
        DataTable dt1 = new DataTable();
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MembershipConfig_Load(object sender, EventArgs e)
        {
            //CheckTable = new DataTable();
            dt1.Columns.Add("Checked");
            dt1.Columns.Add("nodeName");
            dt1.Columns.Add("EXI_ModuleCode");
            dt1.Columns.Add("EF_FeatureName");
            dt1.Columns.Add("EES_ID");
            dt1.Columns.Add("RIU_ID");
            dt1.Columns.Add("CM_ID");
            dt1.Columns.Add("RM_ID");
            dt1.Columns.Add("IsConfig");
            dt1.Columns.Add("EF_ID");
            dt1.Columns.Add("IsBenShen");            
            if (TabName == 0)
            {
                dt = efBll.GetExtractFeatureData(NodeName, 0);
            }
            else if (TabName == 1)
            {
                dt = efBll.GetExtractFeatureData(NodeName, 1);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                dt1 = dt.Clone();
                CheckTable = dt.Clone();
                CheckTable.Columns.Add("IsBenShen");
                dt1.Columns.Add("IsBenShen");
                if (dt.Select(" nodeName='" + NodeName + "' and EXI_ModuleCode='" + MudelName + "' and  EF_FeatureName='" + FileName + "'").Length > 0)
                {
                    DataRow dr = dt.Select(" nodeName='" + NodeName + "' and EXI_ModuleCode='" + MudelName + "' and  EF_FeatureName='" + FileName + "'")[0];
                    dt1.ImportRow(dr);
                    dt1.Rows[0]["Checked"] = false;
                    dt1.Rows[0]["IsBenShen"] = "是";
                }
                else if (dt.Select(" nodeName='" + NodeName + "' and EXI_ModuleCode='" + MudelName + "'").Length > 0)
                {
                    foreach (var item in dt.Select(" nodeName='" + NodeName + "' and EXI_ModuleCode='" + MudelName + "'"))
                    {
                        dt1.ImportRow(item);
                    }
                }
                else if (dt.Select(" nodeName='" + NodeName + "' ").Length > 0)
                {
                    foreach (var item in dt.Select(" nodeName='" + NodeName + "' "))
                    {
                        dt1.ImportRow(item);
                    }
                }
                DataRow dr1 = dt1.NewRow();
                dr1["Checked"] = true;
                dr1["IsConfig"] = "否";
                dr1["nodeName"] = NodeName;
                dr1["EXI_ModuleCode"] = MudelName;
                dr1["EF_FeatureName"] = FileName;
                dt1.Rows.Add(dr1);
            }
            else
            {
                CheckTable = dt1.Clone();
                // select 'false' as Checked,'是' as IsConfig, a.EXI_ParagraCode as nodeName,a.EXI_ModuleCode,b.EF_FeatureName,b.EES_ID,b.RIU_ID,b.CM_ID,b.RM_ID
                DataRow dr = dt1.NewRow();
                dr["Checked"] = true;
                dr["IsConfig"] = "否";
                dr["nodeName"] = NodeName;
                dr["EXI_ModuleCode"] = MudelName;
                dr["EF_FeatureName"] = FileName;
                dt1.Rows.Add(dr);
            }
            dgvRelationConfig.DataSource = dt1;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCanel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dgvRelationConfig.Rows.Count > 0)
            {
             
                int num = 0;
                for (var i = 0; i <= dgvRelationConfig.Rows.Count - 1; i++)
                {
                    var check = (DataGridViewCheckBoxCell)dgvRelationConfig.Rows[i].Cells["CheckRow"];
                    if (check.Value!= DBNull.Value &&Convert.ToBoolean(check.Value))
                    {
                        num++;
                    }
                }
                if (num == 0)
                {
                    MessageBox.Show("请选择需要配置的数据");
                    return;
                }
                else if (num > 1)
                {
                    MessageBox.Show("需要配置的数据只能有一条");
                    return;
                }
                else
                {
                    for (var i = 0; i <= dgvRelationConfig.Rows.Count - 1; i++)
                    {
                        var check = (DataGridViewCheckBoxCell)dgvRelationConfig.Rows[i].Cells["CheckRow"];
                        if (Convert.ToBoolean(check.Value))
                        {
                           
                            DataRow dr = ((DataTable)dgvRelationConfig.DataSource).Rows[i];
                            CheckTable.Clear();
                            CheckTable.ImportRow(dr);
                            break;
                        }
                    }
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }
            }
        }

        /// <summary>
        /// 快捷搜索框改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txbSearch_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txbSearch.Text.Trim()) && dt1.Rows.Count > 0)
            {
                if (dt1.Select("EXI_ModuleCode like '%" + txbSearch.Text.Trim() + "%' ").Length > 0)
                {
                    dgvRelationConfig.DataSource = dt1.Select("EXI_ModuleCode like '%" + txbSearch.Text.Trim() + "%' ").CopyToDataTable<DataRow>();
                    return;
                }

                if (dt1.Select(" EF_FeatureName like '%" + txbSearch.Text.Trim() + "%'").Length > 0)
                {
                    dgvRelationConfig.DataSource = dt1.Select("EF_FeatureName like '%" + txbSearch.Text.Trim() + "%' ").CopyToDataTable<DataRow>();
                    return;
                }
                dgvRelationConfig.DataSource = dt1.Clone();
            }
        }
    }
}
